package cn.com.capinfo.ea.example.demo.domain

import cn.com.capinfo.ea.core.annotation.Title
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import grails.gorm.annotation.Entity
import org.springframework.format.annotation.DateTimeFormat
@Entity
// i8n国际化
@Title(zh_CN = "图书")
@JsonIgnoreProperties(["errors", "metaClass", "dirty", "attached", "dirtyPropertyNames","handler","target","session","entityPersisters","hibernateLazyInitializer","initialized","proxyKey","children"])
class Book {
    @Title(zh_CN = "标题")
    String title
    @Title(zh_CN = "价格")
    double price
    @Title(zh_CN = "页数")
    int pageNumber
    @Title(zh_CN = "出版日期")
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    Date publishDate


    // 属于...  //例子:书属于作者,具有级联操作.
    @Title(zh_CN = "作者")
    static belongsTo = [author:Author]

    // 约束
    // 约束通过定义DSL(领域专用语言)来生成校验规则、架构生成和CRUD生成元数据.
    // https://ea.capinfo.com.cn/document/html5/zh/guide/index-constraints.html
    // 前台校验规则
    static constraints = {
        title (nullable: false,size: 0..10, unique: true, blank:false)
        price(min:0d,max:200d)
        pageNumber(min: 0,)
        publishDate(nullable:false)
    }

    // 与表相关的规则
    // https://ea.capinfo.com.cn/document/html5/zh/guide/index-domain.html
    static mapping = {
        table(name:"Book") //表名
        comment "图书表" //表的注释
        title(index:"BOOK_TITLE_INDEX",comment:"标题");
        price(column: "PRICE_COLUMN",defaultValue : 0)
    }


    String toString(){
        return title
    }
}